package six_ValidParentheses;

import java.util.Stack;

public class CSolution {

    public static void main(String[] args) {
        System.out.println("aa");
        CSolution cSolution = new CSolution();
        System.out.println(cSolution.isValid("{}"));
        System.out.println(cSolution.isValid("()"));
        System.out.println(cSolution.isValid("[]"));
        System.out.println(cSolution.isValid("()[]{}"));
        System.out.println(cSolution.isValid("(]"));
        System.out.println(cSolution.isValid("([)]"));
        System.out.println(cSolution.isValid("{[]}"));
    }

    public boolean isValid(String s) {
        if (s == null || s.equals("")) {
            return true;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            char aChar = getChar(c);
            if (stack.empty() || stack.peek() != aChar) {
                if(c == '}'||c==')'||c==']') {
                    return false;
                }
                stack.push(c);
            } else {
                stack.pop();
            }
        }
        if(stack.empty()) {
            return true;
        }
        return false;
    }

    private char getChar(char c) {
        switch (c) {
            case '}':
                return '{';
            case ')':
                return '(';
            case ']':
                return '[';
            default:
                return 'e';
        }
    }
}
